using Microsoft.AspNetCore.Mvc;
using RepositoryDemo.Domain.Orders;
using RepositoryDemo.Domain.Orders.Repositories;

namespace RepositoryDemo.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class OrderController : ControllerBase
{
    private readonly IOrderRepository _orderRepository;

    public OrderController(IOrderRepository orderRepository)
    {
        _orderRepository = orderRepository;
    }

    [HttpGet("{id}")]
    public async Task<Order> GetAsync(int id)
    {
        return await _orderRepository.GetAsync(id);
    }

    [HttpGet]
    public async Task<List<Order>> GetListAsync()
    {
        return await _orderRepository.GetListAsync();
    }

    [HttpPost]
    public async Task<int> CreateAsync(string name)
    {
        var order = new Order()
        {
            Name = name,
            CreateDate = DateTime.UtcNow,
        };
        order = await _orderRepository.InsertAsync(order);
        return order.Id;
    }

    [HttpPut("{id}")]
    public async Task<int> UpdateAsync(int id, string name)
    {
        var order = await _orderRepository.GetAsync(id);
        order.Name = name;
        order = await _orderRepository.UpdateAsync(order);
        return order.Id;
    }

    [HttpDelete("{id}")]
    public async Task DeleteAsync(int id)
    {
        await _orderRepository.DeleteAsync(id);
    }
}